" Set leader to space
let g:mapleader = ' '

" vim-plug
call plug#begin(has('nvim') ? stdpath('data') . '/plugged' : '~/.vim/plugged')

Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}
Plug 'preservim/nerdtree'
Plug 'tpope/vim-surround'
Plug 'easymotion/vim-easymotion'
Plug 'mg979/vim-visual-multi', {'branch': 'master'}
Plug 'tpope/vim-commentary'
Plug 'vim-scripts/ReplaceWithRegister'
Plug 'vim-scripts/argtextobj.vim'
Plug 'kana/vim-textobj-entire'
Plug 'kana/vim-textobj-user'
Plug 'machakann/vim-highlightedyank'
Plug 'michaeljsmith/vim-indent-object'
Plug 'dbakker/vim-paragraph-motion'
Plug 'tommcdo/vim-exchange'

call plug#end()

" markdown-preview.nvim
" Ctrl+m to toggle preview
nmap <C-m> <Plug>MarkdownPreviewToggle

function! g:OpenMarkdownPreview(url)
"	silent execute "!google-chrome-stable --new-window " . a:url . " &"
	silent execute "!microsoft-edge-beta --new-window " . a:url . " &"
endfunction

let g:mkdp_browserfunc = 'g:OpenMarkdownPreview'

"let g:mkdp_browser = 'microsoft-edge-beta'

"let g:mkdp_auto_start = 1
let g:mkdp_auto_close = 1

" NERDTree settings
" NERDTree shortcuts
noremap <C-t> :NERDTreeToggle<CR>
noremap <leader>t :NERDTreeFocus<CR>
" autocmd for NERDTree
" Exit Vim if NERDTree is the only window left.
" autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() |
    \ quit | endif
" If another buffer tries to replace NERDTree, put it in the other window, and bring back NERDTree.
autocmd BufEnter * if bufname('#') =~ 'NERD_tree_\d\+' && bufname('%') !~ 'NERD_tree_\d\+' && winnr('$') > 1 |
    \ let buf=bufnr() | buffer# | execute "normal! \<C-W>w" | execute 'buffer'.buf | endif

" Indent width
set shiftwidth=4
set tabstop=4

" Show line number
set number

" No wrap scan for searching
set nowrapscan

" scroll off
set scrolloff=2

" focus to window
noremap <leader>h <C-w>h
noremap <leader>j <C-w>j
noremap <leader>k <C-w>k
noremap <leader>l <C-w>l

" scroll to bottom
noremap <leader>g G

" write
noremap <leader>w :w<cr>

" Copy to clipboard
noremap <leader>y "+y
" Paste from clipboard
noremap <leader>p "+p
noremap <leader>P "+P

" Insert empty line after current line
noremap <leader>o o<Esc>
noremap <leader>O O<Esc>

" shortcuts in insert mdoe
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-h> <Left>
inoremap <C-l> <Right>

" shortcuts in command line mode
"cnoremap <C-p> <Up>
"cnoremap <C-n> <Down>

" auto fcitx5 in md files
let g:input_toggle = 1

function! g:InactiveFcitx5()
	let s:input_status = system("fcitx5-remote")
	if s:input_status == 2
		let g:input_toggle = 1
		let l:a = system("fcitx5-remote -c")
	endif
endfunction

function! g:ActiveFcitx5()
	let s:input_status = system("fcitx5-remote")
	if s:input_status != 2 && g:input_toggle == 1
		let g:input_toggle = 0
		let l:a = system("fcitx5-remote -o")
	endif
endfunction

autocmd InsertLeave *.md call InactiveFcitx5()
autocmd InsertEnter *.md call ActiveFcitx5()
" end auto fcitx
